﻿using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;

namespace TimeSolve
{
    public class GraphManageWindowViewModel : BindableBase
    {
        public GraphManageWindowViewModel()
        {
            mainVM = MainViewModel.mainViewModel;

            dcnew = new DelegateCommand<object>(_dcnew);
            dcdel = new DelegateCommand(_dcdel);
            dccopy = new DelegateCommand(_dccopy);
            test = new DelegateCommand(_test);
            apply = new DelegateCommand(_apply);
        }
        #region test
        public DelegateCommand test { get; set; }
        private void _test()
        {

        }
        #endregion

        public MainViewModel mainVM { get; set; }
        //       public Array valueTypess { get; set; } = Enum.GetValues(typeof(ValueTypes));
        public DelegateCommand<object> dcnew { get; set; }
        public DelegateCommand dcdel { get; set; }
        public DelegateCommand dccopy { get; set; }
        public DelegateCommand apply { get; set; }
        private void _dcnew(object o)
        {
            string swi = (string)o;
            GraphTypes gt = GraphTypes.网格图;
            string namehead = "";
            switch (swi)
            {
                case "wanggetu":
                    gt = GraphTypes.网格图;
                    namehead = "网格图 ";
                    break;
                case "yuntu":
                    gt = GraphTypes.等值图;
                    namehead = "云图 ";
                    break;
                case "shiliangtu":
                    gt = GraphTypes.矢量图;
                    namehead = "矢量图 ";
                    break;
                case "jixiantu":
                    gt = GraphTypes.迹线图;
                    namehead = "迹线图 ";
                    break;
                default:
                    break;
            }
            //string newname = "";
            for (int i = 1; i < 10000; i++)
            {
                //newname = namehead + i;
                string newname = string.Format("{0} {1}", namehead, i);
                if (_isValidName(newname))
                {
                    GraphItem gi = new GraphItem(gt) { name = newname };
                    mainVM.graphs.Add(gi);
                    return;
                }
            }
            mainVM.log.AddWithTime("创建图形失败，无可用名称");
        }
        private void _dcdel()
        {

        }
        private void _dccopy()
        {

        }

        private bool _isValidName(string s)
        {
            foreach (var g in mainVM.graphs)
            {
                if (s == g.name)
                {
                    return false;
                }
            }
            return true;
        }
        private void _apply()
        {
            foreach (GraphItem gi in mainVM.graphs)
            {
                gi.updateFully();
            }

        }

    }


}
